package com.applet.code.util;

import com.github.pagehelper.PageHelper;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.FieldDefaults;

import java.util.List;

@Getter
@Setter
@FieldDefaults(level=AccessLevel.PRIVATE)
public class Page extends Entity {

	/**
	 * 序列化版本标示
	 */
	private static final long serialVersionUID = 1L;

	// 页码数
	Integer page;
	
	// 单页最大行数
	Integer limit;

	List<?> list;

	String field;

	String order;

	Long total;
	
	@SuppressWarnings("rawtypes")
	private void validateOrderField(boolean isPage, Class cls) {
		if (isPage) {
			PageHelper.startPage(page == null ? 1 : page, limit == null ? 10: limit);
		}
		if (order != null && cls != null) {

        }
	}
	
	public void initPagingData(Class<?> cls){
		this.validateOrderField(true, cls);
	}

	public void initAllData(Class<?> cls){
		this.validateOrderField(false, cls);
	}
	
	public Page setResult(List<?> result){
        com.github.pagehelper.Page<?> page = (com.github.pagehelper.Page<?>) result;
        if(page !=null){
			this.total = page.getTotal();
			this.list = page.getResult();
		}

        return this;
    }

}
